package com.baiqian.doupo.item;

import com.baiqian.doupo.DouPo;
import com.baiqian.doupo.Utils;
import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import com.baiqian.doupo.modgroup.DouPoModGroup;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nullable;
import java.util.List;

public class GongFa extends Item{
    String name;
    String zh_name;
    public GongFa(Properties properties) {
        super(properties);
    }
    protected void setName(String name,String zh_name){
        this.name=name;
        this.zh_name=zh_name;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (!worldIn.isRemote && handIn == Hand.MAIN_HAND) {
            LazyOptional<IDouQiCapability> douqiCap = playerIn.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
            if (douqiCap.isPresent()) {
                douqiCap.ifPresent((l) -> {
                            if(!DouPo.Debug&&l.getLevelBig()<2){
                                playerIn.sendMessage(
                                        new TranslationTextComponent("message." + Utils.MOD_ID + ".gongfa.level_not"),
                                        playerIn.getUniqueID()
                                );
                            }
                            else {
                                boolean flag = false;
                                for (com.baiqian.doupo.gongfa.GongFa gong_fa : l.getGongFas())
                                    if (gong_fa.gongfa_name.equals(name)) {
                                        flag = true;
                                        break;
                                    }
                                if (flag) {
                                    playerIn.sendMessage(
                                            new TranslationTextComponent("message." + Utils.MOD_ID + ".gongfa.use_repeat",
                                                    new TranslationTextComponent("item." + Utils.MOD_ID + ".gongfa_"+zh_name).getString()
                                            ),
                                            playerIn.getUniqueID()
                                    );
                                } else {
                                    com.baiqian.doupo.gongfa.GongFa gong_fa = com.baiqian.doupo.gongfa.GongFa.getGongFa(name);
                                    if (DouPo.Debug||l.getSoulProperty()[gong_fa.gongfa_property]) {
                                        l.addGongFa(gong_fa);
                                        playerIn.sendMessage(
                                                new TranslationTextComponent("message." + Utils.MOD_ID + ".gongfa.use",
                                                        new TranslationTextComponent("item." + Utils.MOD_ID + ".gongfa_"+zh_name).getString()
                                                ),
                                                playerIn.getUniqueID());
                                    } else {
                                        playerIn.sendMessage(
                                                new TranslationTextComponent("message." + Utils.MOD_ID + ".gongfa.use_unsoul",
                                                        new TranslationTextComponent("item." + Utils.MOD_ID + ".gongfa_"+zh_name).getString()
                                                ),
                                                playerIn.getUniqueID());
                                    }
                                }
                            }
                        }
                );
            }

        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }
}

//功法狂狮怒罡
class GongFaKuangShiNuGang extends GongFa {
    public GongFaKuangShiNuGang() {
        super(new Properties().group(DouPoModGroup.douPoGongFaGroup).maxStackSize(1));
        setName("狂狮怒罡","kuang_shi_nu_gang");
    }

    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(new TranslationTextComponent(this.getTranslationKey() + ".1"));
        tooltip.add(new TranslationTextComponent(this.getTranslationKey() + ".2"));
    }

}

//功法青木剑诀
class GongFaQingMuJianJue extends GongFa {
    public GongFaQingMuJianJue() {
        super(new Properties().group(DouPoModGroup.douPoGongFaGroup).maxStackSize(1));
        setName("青木剑诀","qing_mu_jian_jue");
    }

    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(new TranslationTextComponent(this.getTranslationKey() + ".1"));
        tooltip.add(new TranslationTextComponent(this.getTranslationKey() + ".2"));
    }

}